package com.linkage.educloud.ah.mvp.presenter.dynamic;

import android.content.Context;

import com.linkage.educloud.ah.Constants;
import com.linkage.educloud.ah.http.Method;
import com.linkage.educloud.ah.http.RequestUtil;
import com.linkage.educloud.ah.mvp.contract.dynamic.IDynamicWriteContract;
import com.linkage.educloud.ah.mvp.model.dynamic.DynamicWriteModel;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.subsciber.BaseSubscriber;
import com.zzhou.common.library.util.ToastShow;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.List;

import io.reactivex.Observable;

/**
 * Created by Mr'Tang on 2018/11/21.
 */

public class DynamicWritePresenter extends IDynamicWriteContract.IPresenter<IDynamicWriteContract.IView> {
    Context mContext;
    IDynamicWriteContract.IModel iModel;

    public DynamicWritePresenter(Context context, IDynamicWriteContract.IView iView) {
        super(iView);
        mContext = context;
        iModel = new DynamicWriteModel();
    }

    //获取班级主题
    @Override
    public void getTheme(String clazz_id, String version) {
        Observable observable = iModel.request(Method.POST, true, RequestUtil.getTheme(clazz_id, version));
        observable.compose(iView.bindLifecycle())
                .subscribe(new BaseSubscriber<String>() {
                    @Override
                    public void onNext(String s) {
                        //{"ret":0,"theme_content":"#人气班级冲冲冲#","theme_title":"#人气班级冲冲冲#","msg":"获得最新话题成功"}
                        try {
                            JSONObject json = new JSONObject(s);
                            if (json.getInt("ret") == Constants.Result.ret) {
                                ((IDynamicWriteContract.IView) iView).changeTheme(json.getString("theme_content"));
                            } else {
                                ToastShow.show(mContext, json.getString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            ToastShow.show(mContext, e.getMessage());
                        }
                    }

                    @Override
                    public void onError(ApiException e) {

                    }
                });
    }

    //发送动态
    @Override
    public void senddynamicmsg(String clazz_id, final Long userid, int type, int usertype, final String content, String uploadimage, int photo_sync, int notice_sync) {
        Observable observable = iModel.request(Method.POST, true, RequestUtil.senddynamicmsg("", clazz_id, userid, type, usertype, content, uploadimage, photo_sync, notice_sync));
        observable.compose(iView.bindLifecycle())
                .subscribe(new BaseSubscriber<String>() {
                    @Override
                    public void onNext(String s) {
                        //{"c_username":"","comment_id":"1579","result":1,"desc":"操作成功"}
                        try {
                            JSONObject json = new JSONObject(s);
                            if (json.getInt("result") == Constants.Result.result) {
                                ToastShow.show(mContext, "发送成功");
                                ((IDynamicWriteContract.IView) iView).addSuccess();
                            } else {
                                ToastShow.show(mContext, json.getString("desc"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            ToastShow.show(mContext, e.getMessage());
                        }
                    }

                    @Override
                    public void onError(ApiException e) {

                    }
                });
    }

    //发送动态
    @Override
    public void senddynamicattachment(int type, String clazz_id, File file) {
        Observable observable = iModel.request(true, RequestUtil.senddynamicattachment(type, clazz_id), file);
        observable.compose(iView.bindLifecycle())
                .subscribe(new BaseSubscriber<String>() {
                    @Override
                    public void onNext(String s) {
                        //{"result":1,"desc":"上传文件为成功","path":"pic\/6321d6da-a1b4-40d6-819a-e29709a6d2a2\/small\/20181122142835a6335bdf-43cc-4eaa-9229-13d73187f836.jpg"}
                        try {
                            JSONObject json = new JSONObject(s);
                            if (json.getInt("result") == Constants.Result.result) {

                            } else {
                                ToastShow.show(mContext, json.getString("desc"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            ToastShow.show(mContext, e.getMessage());
                        }
                    }

                    @Override
                    public void onError(ApiException e) {

                    }
                });
    }
}
